package com.tyae.conversor;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import com.tyae.bean.Comarca;
import com.tyae.dao.DAOFactory;


@FacesConverter(value="comarcaConverte", forClass=Comarca.class) 
public class ComarcaConverte implements Converter {

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
		if(arg2.equals("0")){
			Comarca comarca = new Comarca();
			return comarca;
		}			
		return DAOFactory.getComarcaDAO().getComarca(Integer.valueOf(arg2)).clone();
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
		return String.valueOf(((Comarca) arg2).getCodigoComarca());
	}

}
